/*! \file StdFileSystem.cpp
 *	\brief 
 *
 *	created:	2010/07/27
 *	created:	27:7:2010   17:42
 *	filename: 	C:\src\tactics-engine\libs\io\StdFileSystem.cpp
 *	file path:	C:\src\tactics-engine\libs\io
 *	file base:	StdFileSystem
 *	file ext:	cpp
 *	author:		Vitalii Trubchaninov
 *				venngraydog@gmail.com
 */
#include "io/stdafx.h"
#include "io/StdFileSystem.h"

#include <fstream>

#include "utils/UnicodeConverter.h"

namespace tt{
namespace io{

StdFileSystem::StdFileSystem()
: m_root()
{

}

StdFileSystem::StdFileSystem( const PathW& root )
: m_root( utils::to_ansi_1024(root.GetString()) )
{

}

StdFileSystem::~StdFileSystem()
{

}

SharedPtr<BaseIStream> StdFileSystem::OnGetIStream( const Path& path )
{
	Path fullPath = m_root / path;
	SharedPtr<std::ifstream> is( new std::ifstream(fullPath.GetString().c_str(), std::ios_base::binary) );
	if(is->fail())
		return SharedPtr<BaseIStream>();
	return is;
}

SharedPtr<BaseOStream> StdFileSystem::OnGetOStream( const Path& path )
{
	Path fullPath = m_root / path;
	SharedPtr<std::ofstream> os( new std::ofstream(fullPath.GetString().c_str(), std::ios_base::binary) );
	if(os->fail())
		return SharedPtr<BaseOStream>();
	return os;
}

} //namespace io

//////////////////////////////////////////////////////////////////////////
io::StdFileSystem& ttDefaultFileSystem()
{
	static io::StdFileSystem s_DefaultFileSystem;
	return s_DefaultFileSystem;
}

} //namespace tt